//
//  WXSDreamTalkBubble.m
//  SleepBooks
//
//  Created by 王勇 on 2016/9/23.
//  Copyright © 2016年 rimi. All rights reserved.
//

#import "WXSDreamTalkBubble.h"

@interface WXSDreamTalkBubble ()

@property (nonatomic,strong)UIImageView *bubbleImageView;
@property (nonatomic,strong)UIButton *button;
@property (nonatomic,assign)NSInteger bubbleWidth;
@end

@implementation WXSDreamTalkBubble

+(instancetype)bubble{
    WXSDreamTalkBubble *bubble = [[WXSDreamTalkBubble alloc]init];
    bubble.frame = CGRectMake(0, 0, 80, 40);
    return  bubble;
}


-(void)initializeAppearence{
    UIImageView *bubbleImageView = [[UIImageView alloc]initWithFrame:self.bounds];
    UIImage *image = [UIImage imageNamed:@"梦话气泡"];
    
    UIImage *modifiedImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
    
    bubbleImageView.image = modifiedImage;
    bubbleImageView.userInteractionEnabled = YES;
    [self addSubview:bubbleImageView];
    
    UIButton *button = [[UIButton alloc]init];
    button.frame = self.bounds;
    [button addTarget:self action:@selector(bubblePressed) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    
    self.bubbleImageView = bubbleImageView;
    self.button = button;
}

-(void)layoutSubviews{
//    UIImageView *bubbleImageView = [[UIImageView alloc]initWithFrame:self.bounds];
//    UIImage *image = [UIImage imageNamed:@"梦话气泡"];
//
//    UIImage *modifiedImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
//    
//    bubbleImageView.image = modifiedImage;
//    bubbleImageView.userInteractionEnabled = YES;
//    [self addSubview:bubbleImageView];
//    [self setBackgroundColor:[UIColor redColor]];
//    UIButton *button = [[UIButton alloc]init];
//    button.frame = self.bounds;
//    [button addTarget:self action:@selector(bubblePressed) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:button];
//    
//    self.bubbleImageView = bubbleImageView;
//    self.button = button;
}

-(void)bubblePressed{
    if ([self.delegate respondsToSelector:@selector(dreamTalkBubbleDidPressed:)]) {
        [self.delegate dreamTalkBubbleDidPressed:self];
    }
}
#pragma mark - Private
-(NSInteger)calculateLength{
    double length = self.model.recordDuration;
    if (length <=5) {
        length = 40;
    }else if (length > 5 && length <= 10){
        length = 60;
    }else if (length > 10 && length <= 20){
        length = 80;
    }else if (length >20 && length <= 40){
        length = 120;
    }else if (length > 40 && length < 60){
        length = 150;
    }else{
        length = 180;
    }
    
    return (NSInteger)length;
}


#pragma mark Setter & Getter
-(void)setModel:(WXSDreamTalkModel *)model{
    _model = model;
    self.bubbleWidth = [self calculateLength];
    self.frame = CGRectMake(0, 0, self.bubbleWidth, 37);
    [self initializeAppearence];
}
@end
